﻿/**********************************************************
*-----------------------------------*
* flareJ JavaScript Library
*-----------------------------------*
* 作者:Joe_Sky
* 邮箱:hitomi_doax@tom.com
* 最后更新时间:2011-7-7
*-----*-----*-----*-----*-----*-----*
* 基于jQuery
***********************************************************/

/*----------------------------------------------*
* flareJ.Pagination
*-------*-------*-------*-------*-------*-------*
* 分页控件
*-------*-------*-------*-------*-------*-------*
* (必须)需引入flareJ.Base.js
*----------------------------------------------*/
(function ($) {
    var FJ = $.flareJ, flareJ = FJ;

    /************************************************************
    *-----------------------------------------------------------*
    *                         分页控件
    *-----------------------------------------------------------*
    *************************************************************
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 分页形式为后台分页
    *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
    * 调用方法:$("#div").PNJ();
    ************************************************************/
    this.PaginationJ = this.PNJ = FJ.PaginationJ = FJ.PNJ = FJ.CJ.extend({
        //#region 构造方法
        init: function (elemObj, settings) {
            //参数
            this._super(elemObj, $.extend(true, {
                fjType: "PNJ",
                renderTo: elemObj,                          //要加载到的容器
                widthF: "auto",                           //最大宽度
                heightF: "16px",                          //最大高度
                style: "",
                borderWidth: 0,
                dataCount: 0,                           //数据总数
                pageSize: 10,                            //每页数据数
                pageCount: 0,                           //总页数
                curPage: 0,                             //当前页码
                cpWidth: 24,                            //当前页文本框宽度
                sort: "",                               //排序字段
                dir: "ASC",                              //排序方向
                showOnInit: true,
                url: "/flareJ.Java/WebRoot/JsLibrary/flareJ/utility/flareJ_DatabaseTest.ashx",  //后台获取数据页面url
                urlData: {
                    type: "Get"
                },
                btnGoName: "",                          //跳转按钮上的字
                hasPages: true,                         //是否显示页数链接
                showDataCount: false,                   //显示信息
                showPageSize: false,
                showPageCount: false,
                colorParams: {
                    bgColor: "#fff"                     //背景色
                },
                evts: {
                    OnPaging: null,                         //分页中事件
                    preOnPage: null,                        //执行分页前事件
                    finOnPage: null                         //执行分页完成事件
                }
            }, settings));

            this.initFn();

            return this;
        },
        //#endregion

        //#region 初始化
        initFn: function () {
            this._super();
            this.create();

            //设置信息是否显示
            if(!this.p.showDataCount){
                this.pnjDataCount.hide();
            }
            if(!this.p.showPageSize){
                this.pnjPageSize.hide();
            }
            if(!this.p.showPageCount){
                this.pnjPageCount.hide();
            }

            //默认加载数据
            if (this.p.showOnInit) {
                this.show();
            }
            else{
                this.setVisible(false);
            }
        },
        //#endregion

        //#region 构建
        create: function () {
            this._super();
            var thiz = this;

            //外层
            this.divOut.addClass("pnj").attr("id", "PNJ_" + this.objId).css({
                height: this.p.heightF,
                width: this.p.widthF
            }).attr("style", this.p.style);

            //数据总数
            this.pnjDataCount = $('<div class="pnj_info"></div>');
            this.pnjDataCountN = $('<span>' + this.p.dataCount + '</span>');
            this.pnjDataCount.append("共").append(this.pnjDataCountN).append("条数据");

            //每页数据数
            this.pnjPageSize = $('<div class="pnj_info"></div>');
            this.pnjPageSizeN = $('<span>' + this.p.pageSize + '</span>');
            this.pnjPageSize.append("每页").append(this.pnjPageSizeN).append("条");

            //总页数
            this.pnjPageCount = $('<div class="pnj_info"></div>');
            this.pnjPageCountN = $('<span>' + this.p.pageCount + '</span>');
            this.pnjPageCount.append("共").append(this.pnjPageCountN).append("页");

            //分割线
            this.pnjSplit = '<div class="pnj_split"></div>';

            //第一页
            this.pnjFirst = $('<div class="pnj_btn pnj_btn_first" title="第一页"></div>').bind("click", function () {
                if (thiz.pnjFirst.hasClass("pnj_btn_first")) {
                    thiz.onPage(1);
                }
            }).addClass("pnj_btn_first_g");

            //前一页
            this.pnjPrev = $('<div class="pnj_btn pnj_btn_prev" title="前一页"></div>').bind("click", function () {
                if (thiz.pnjPrev.hasClass("pnj_btn_prev")) {
                    thiz.onPage(parseFloat(thiz.p.curPage) - 1);
                }
            }).addClass("pnj_btn_prev_g");

            //下一页
            this.pnjNext = $('<div class="pnj_btn pnj_btn_next" title="下一页"></div>').bind("click", function () {
                if (thiz.pnjNext.hasClass("pnj_btn_next")) {
                    thiz.onPage(parseFloat(thiz.p.curPage) + 1);
                }
            }).addClass("pnj_btn_next_g");

            //最后页
            this.pnjLast = $('<div class="pnj_btn pnj_btn_last" title="最后页"></div>').bind("click", function () {
                if (thiz.pnjLast.hasClass("pnj_btn_last")) {
                    thiz.onPage(thiz.p.pageCount);
                }
            }).addClass("pnj_btn_last_g");

            //刷新
            this.pnjRefresh = $('<div class="pnj_btn pnj_btn_refresh" title="刷新"></div>').bind("click", function () {
                thiz.onPage(thiz.p.curPage);
            });

            //当前页
            this.pnjTxt = $('<div class="pnj_txt"></div>');
            this.pnjCurPage = $('<input style="float:left;" type="text" value="' + this.p.curPage + '" />').bind("focus", function () {
                var thiz = this;
                setTimeout(function(){
                    thiz.select();
                }, 50);
            }).bind("blur", function () {
                if (thiz.p.dataCount > 0) {
                    if (!FJ.RX.ep.num.test(this.value)) {  //必须输入大于0的正整数
                        this.value = 1;
                    }
                }
            }).width(this.p.cpWidth);
            this.pnjGo = $('<div class="pnj_btn_go">' + this.p.btnGoName + '</div>').bind("click", function () {
                if (thiz.p.dataCount > 0) {
                    var cNum = parseFloat(thiz.pnjCurPage.val());
                    if (cNum <= parseFloat(thiz.p.pageCount)) {
                        thiz.onPage(cNum);
                    }
                    else {   //输入大于总页数时跳转到最后页
                        thiz.onPage(thiz.p.pageCount);
                    }
                }
            });
            this.pnjTxt.append('<div class="pnj_txt_div">当前第</div>').append(this.pnjCurPage).append('<div class="pnj_txt_div">页</div>').append(this.pnjGo);

            //页数链接
            if (this.p.hasPages) {
                this.pnjPages = $('<div class="pnj_pages"></div>');

                //加载
                this.divOut.append(this.pnjDataCount).append(this.pnjPageSize).append(this.pnjPageCount).append(this.pnjFirst).append(this.pnjPrev).append(this.pnjPages).append(this.pnjNext).append(this.pnjLast).append(this.pnjSplit).append(this.pnjTxt).append(this.pnjSplit).append(this.pnjRefresh);
            }
            else {
                //加载
                this.divOut.append(this.pnjDataCount).append(this.pnjPageSize).append(this.pnjPageCount).append(this.pnjFirst).append(this.pnjPrev).append(this.pnjSplit).append(this.pnjTxt).append(this.pnjSplit).append(this.pnjNext).append(this.pnjLast).append(this.pnjSplit).append(this.pnjRefresh);
            }

            return this;
        },
        //#endregion

        //#region 显示
        show: function () {
            this._super();
            this.onPage(1);
            return this;
        },
        //#endregion

        //#region 执行分页事件
        onPage: function (page) {
            var thiz = this;
            this.pnjRefresh.addClass("pnj_btn_refresh_l");

            //执行分页前事件
            this.fire("preOnPage");

            //修改参数
            this.p.urlData = $.extend(this.p.urlData, {
                curPage: page,
                pageSize: this.p.pageSize,
                sort: this.p.sort,
                dir: this.p.dir
            });

            //分页中事件
            this.fire("OnPaging", { page: page });
        },
        //#endregion

        //#region 更新分页数据
        update: function (page, data, count, total) {
            var thiz = this;

            thiz.p.curPage = page;
            thiz.p.dataCount = total;
            if (count > 0) {
                thiz.pnjCurPage.val(page);
            }
            else {
                thiz.pnjCurPage.val(0);
            }

            this.pnjDataCountN.text(this.p.dataCount);
            this.pnjPageSizeN.text(this.p.pageSize);

            //计算总页数
            this.p.pageCount = parseInt(this.p.dataCount % this.p.pageSize > 0 ? this.p.dataCount / this.p.pageSize + 1 : this.p.dataCount / this.p.pageSize);
            this.pnjPageCountN.text(this.p.pageCount);

            //设置按钮状态
            if (count > 0) {
                this.pnjFirst.removeClass("pnj_btn_first_g").addClass("pnj_btn_first");
                this.pnjPrev.removeClass("pnj_btn_prev_g").addClass("pnj_btn_prev");
                this.pnjNext.removeClass("pnj_btn_next_g").addClass("pnj_btn_next");
                this.pnjLast.removeClass("pnj_btn_last_g").addClass("pnj_btn_last");

                if (this.p.pageCount <= 1) {  //只有一页
                    this.pnjFirst.removeClass("pnj_btn_first").addClass("pnj_btn_first_g");
                    this.pnjPrev.removeClass("pnj_btn_prev").addClass("pnj_btn_prev_g");
                    this.pnjNext.removeClass("pnj_btn_next").addClass("pnj_btn_next_g");
                    this.pnjLast.removeClass("pnj_btn_last").addClass("pnj_btn_last_g");
                }
                else if (this.p.curPage == 1) {  //首页
                    this.pnjFirst.removeClass("pnj_btn_first").addClass("pnj_btn_first_g");
                    this.pnjPrev.removeClass("pnj_btn_prev").addClass("pnj_btn_prev_g");
                }
                else if (this.p.curPage == this.p.pageCount) {  //尾页
                    this.pnjNext.removeClass("pnj_btn_next").addClass("pnj_btn_next_g");
                    this.pnjLast.removeClass("pnj_btn_last").addClass("pnj_btn_last_g");
                }

                if (this.p.hasPages) {  //页数链接
                    this.pnjPages.empty();

                    if (this.p.pageCount <= 10) {  //页面总数小于等于10，全部显示
                        for (var i = 1; i <= this.p.pageCount; i++) {
                            (function (pa) {
                                thiz.pnjPages.append($("<span title='第" + i + "页' " + (i == thiz.p.curPage ? "style='color:red;'" : "") + " >" + i + "</span>").click(function () {
                                    thiz.onPage(pa);
                                }));
                            })(i);
                        }
                    }
                    else {
                        //当前页码 <=  4,左侧显示所有 +  当前页码  +  右侧2个页码 + ... + 尾页
                        if (this.p.curPage <= 4) {
                            for (var i = 1; i <= this.p.curPage + 2; i++) {
                                (function (pa) {
                                    thiz.pnjPages.append($("<span title='第" + i + "页' " + (i == thiz.p.curPage ? "style='color:red;'" : "") + " >" + i + "</span>").click(function () {
                                        thiz.onPage(pa);
                                    }));
                                })(i);
                            }
                            this.pnjPages.append($("<span>...</span>").click(function () {
                                thiz.onPage(parseFloat(thiz.p.curPage) + 1);
                            }));
                            this.pnjPages.append($("<span title='第" + thiz.p.pageCount + "页' " + (thiz.p.pageCount == thiz.p.curPage ? "style='color:red;'" : "") + " >" + this.p.pageCount + "</span>").click(function () {
                                thiz.onPage(thiz.p.pageCount);
                            }));
                        }
                        //当前页码 > 4 且<= 页面总数(n) - 3,首页  + ... + 左侧2个页码  + 当前页码  + 右侧2个页码  + ...  +  尾页
                        else if (this.p.curPage > 4 && this.p.curPage <= this.p.pageCount - 3) {
                            this.pnjPages.append($("<span title='第1页' " + (1 == thiz.p.curPage ? "style='color:red;'" : "") + " >1</span>").click(function () {
                                thiz.onPage(1);
                            }));
                            this.pnjPages.append($("<span>...</span>").click(function () {
                                thiz.onPage(parseFloat(thiz.p.curPage) - 1);
                            }));
                            for (var i = this.p.curPage - 2; i <= this.p.curPage + 2; i++) {
                                (function (pa) {
                                    thiz.pnjPages.append($("<span title='第" + i + "页' " + (i == thiz.p.curPage ? "style='color:red;'" : "") + " >" + i + "</span>").click(function () {
                                        thiz.onPage(pa);
                                    }));
                                })(i);
                            }
                            this.pnjPages.append($("<span>...</span>").click(function () {
                                thiz.onPage(parseFloat(thiz.p.curPage) + 1);
                            }));
                            this.pnjPages.append($("<span title='第" + thiz.p.pageCount + "页' " + (thiz.p.pageCount == thiz.p.curPage ? "style='color:red;'" : "") + " >" + this.p.pageCount + "</span>").click(function () {
                                thiz.onPage(thiz.p.pageCount);
                            }));
                        }
                        //当前页码 > 页面总数(n) - 3,首页 + ... +  左侧2个页面 +  当前页码  +  右侧显示所有
                        else if (this.p.curPage > this.p.pageCount - 3) {
                            this.pnjPages.append($("<span title='第1页' " + (1 == thiz.p.curPage ? "style='color:red;'" : "") + " >1</span>").click(function () {
                                thiz.onPage(1);
                            }));
                            this.pnjPages.append($("<span>...</span>").click(function () {
                                thiz.onPage(parseFloat(thiz.p.curPage) - 1);
                            }));
                            for (var i = this.p.curPage - 2; i <= this.p.pageCount; i++) {
                                (function (pa) {
                                    thiz.pnjPages.append($("<span title='第" + i + "页' " + (i == thiz.p.curPage ? "style='color:red;'" : "") + " >" + i + "</span>").click(function () {
                                        thiz.onPage(pa);
                                    }));
                                })(i);
                            }
                        }
                    }
                }
            }
            else {  //没有数据
                this.pnjFirst.removeClass("pnj_btn_first").addClass("pnj_btn_first_g");
                this.pnjPrev.removeClass("pnj_btn_prev").addClass("pnj_btn_prev_g");
                this.pnjNext.removeClass("pnj_btn_next").addClass("pnj_btn_next_g");
                this.pnjLast.removeClass("pnj_btn_last").addClass("pnj_btn_last_g");
                if (this.p.hasPages) {
                    this.pnjPages.empty();
                }
            }

            //执行分页后事件
            this.fire("finOnPage", { data: data });

            this.pnjRefresh.removeClass("pnj_btn_refresh_l");
        },
        //#endregion

        //#region 刷新
        refresh: function (page) {
            if (page == null) {
                this.onPage(this.p.curPage);
            }
            else {
                this.onPage(page);
            }
        },
        //#endregion

        //#region 显示/隐藏
        setVisible: function (isShow) {
            if (isShow) {
                this.divOut.show();
            }
            else {
                this.divOut.hide();
            }
        }
        //#endregion
    });

    //#region 绑定到jquery
    $.fn.extend({
        PaginationJ: function (settings) {
            return new FJ.PaginationJ(this, settings);
        },
        PNJ: function (settings) {
            return $(this).PaginationJ(settings);
        }
    });
    //#endregion

    //#region 图片文件夹路径
    FJ.PNJ.imgSrc = FJ.imgPath + "Pagination/";
    //#endregion

    //#region 加载css
    $("head").append('<link id="PaginationJStyle" href="' + FJ.cssPath + 'flareJ.Pagination.css" rel="stylesheet" type="text/css" />');
    //#endregion
})(jQuery);